﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCDrugStoreEdit : System.Web.UI.UserControl
	{
		private IDrugStoreProvider _DrugStoreProvider;

		public int DrugStoreID
		{
			get
			{
				var id = 0;

				if (ViewState["DrugStoreID"] != null)
				{
					id = (int)ViewState["DrugStoreID"];
				}
				else
				{
					id = CommonHelper.QueryStringInt("id");
				}

				return id;
			}
			set
			{
				ViewState["DrugStoreID"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindDrugStore();
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			if (_DrugStoreProvider == null)
			{
				_DrugStoreProvider = new DrugStoreProvider();
			}

			if (DrugStoreID > 0)
			{
				var theDrugStore = _DrugStoreProvider.GetDrugStore(DrugStoreID);

				if (theDrugStore != null)
				{
					theDrugStore.DrugStoreName = txtDrugStoreName.Text.Trim();
					theDrugStore.DrugStoreDescription = txtDrugStoreDescription.Text.Trim();
					theDrugStore.LastUpdateTime = DateTime.Now;

					if (fuStorePic.HasFile)
					{
						theDrugStore.DrugStorePic = fuStorePic.FileBytes;
					}

					if (_DrugStoreProvider.UpdateDrugStore(theDrugStore))
					{
						ltrMsg.Text = "更新成功";
						BindDrugStore();
					}
					else
					{
						ltrMsg.Text = "更新失败了";
					}
				}
			}
			else
			{
				var theNewDrugStore = new DrugStore() { AddTime = DateTime.Now, DrugStoreName = txtDrugStoreName.Text.Trim(), DrugStoreDescription = txtDrugStoreDescription.Text.Trim() };

				if (fuStorePic.HasFile)
				{
					theNewDrugStore.DrugStorePic = fuStorePic.FileBytes;
				}

				var msg = string.Empty;
				theNewDrugStore = _DrugStoreProvider.CreateDrugStore(theNewDrugStore, out msg);
				if (theNewDrugStore != null)
				{
					ltrMsg.Text = "创建成功";
					DrugStoreID = theNewDrugStore.DrugStoreID;
					BindDrugStore();
				}
				else
				{
					ltrMsg.Text = msg;
				}
			}
		}

		private void BindDrugStore()
		{
			var u = SiteContext.Current.CurrentUser;

			if(DrugStoreID > 0)
			{
				if(_DrugStoreProvider == null)
				{
					_DrugStoreProvider = new DrugStoreProvider();
				}

				var theDrugStore = _DrugStoreProvider.GetDrugStore(DrugStoreID);

				if(theDrugStore != null)
				{
					imgDrugStorePic.Visible = true;

					txtDrugStoreName.Text = theDrugStore.DrugStoreName;
					txtDrugStoreDescription.Text = theDrugStore.DrugStoreDescription;

					if(theDrugStore.DrugStorePic == null)
					{
						imgDrugStorePic.ImageUrl = "~/template/nopic.gif";
						imgDrugStorePic.Width = 100;
						imgDrugStorePic.Height = 85;
					}
					else
					{
						imgDrugStorePic.ImageUrl = "~/ImageHandler.ashx?picturesize=100&drugstoreid=" + theDrugStore.DrugStoreID.ToString();
					}
				}
			}
			else if(u != null && u.Role != null && u.Role.RoleName.ToLower().Equals("storemanager"))
			{
				hlCancel.Visible = false;

				if(_DrugStoreProvider == null)
				{
					_DrugStoreProvider = new DrugStoreProvider();
				}

				var theDrugStore = _DrugStoreProvider.GetDrugStore(u.DrugStoreID.GetValueOrDefault(0));

				if(theDrugStore != null)
				{
					this.DrugStoreID = u.DrugStoreID.GetValueOrDefault(0);

					imgDrugStorePic.Visible = true;

					txtDrugStoreName.Text = theDrugStore.DrugStoreName;
					txtDrugStoreDescription.Text = theDrugStore.DrugStoreDescription;

					if(theDrugStore.DrugStorePic == null)
					{
						imgDrugStorePic.ImageUrl = "~/template/nopic.gif";
						imgDrugStorePic.Width = 100;
						imgDrugStorePic.Height = 85;
					}
					else
					{
						imgDrugStorePic.ImageUrl = "~/ImageHandler.ashx?picturesize=100&drugstoreid=" + theDrugStore.DrugStoreID.ToString();
					}
				}
			}
			else
			{
				imgDrugStorePic.Visible = false;
			}
		}
	}
}